package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-07-12
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {
    private EduVideoService eduVideoService;
    @Autowired
    public void setEduVideoService(EduVideoService eduVideoService) {
        this.eduVideoService = eduVideoService;
    }
    private VodClient vodClient;
    @Autowired
    public void setVodClient(VodClient vodClient) {
        this.vodClient = vodClient;
    }

    @PostMapping("/addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.save(eduVideo);
        return R.ok();
    }

    @DeleteMapping("/{videoId}")
    public R deleteVideo(@PathVariable String videoId){
        // 根据小节id来获取视频id
        String videoSourceId = eduVideoService.getById(videoId).getVideoSourceId();
        if(!StringUtils.isEmpty(videoSourceId)) {
            // 调用暴露接口服务的删除视频方法(远程调用)
            R result = vodClient.deleteVideoById(videoSourceId);
            if(result.getCode() == 20001){
                throw new GuliException(20001,"删除视频失败,熔断器....");
            }
        }
        // 删除小节信息
        eduVideoService.removeById(videoId);
        return R.ok();
    }
}

